'use strict';

import React, { Component } from 'react';

import {
  StyleSheet,
  View,
  ScrollView,
  TouchableOpacity,
  Text,
  Keyboard
} from 'react-native';

import Config from '../../config';
import SizeUtils from '../../common/SizeUtils';
import CommonStyles from '../../common/CommonStyles';
import TImage from '../../components/TImage';
import BaseItem from '../common/BaseItem';
import NewsConstants from '../../common/NewsConstants';
import NewsVideoView from '../news/NewsVideoView';
import CommonShowViewFullScreenAction from '../../actions/common/CommonShowViewFullScreenAction';
import {DateUtils} from '../../utils/Utils';
import ActivityListItemView from '../activity/ActivityListItemView';
import HeartLoveItemView from '../heart/HeartLoveItemView';

export default class GongGaoItemView extends Component {
    render() {
      const {data, index,type }=this.props;
      return <Item data={data} key={index} number={index} />;
    }
}

class Item extends BaseItem {
  constructor(props) {
    super(props);
    this.state={
      readers_num:this.props.data.readers_num
    };
  }
  render() {
    const { number,data } = this.props;
    return (
      <TouchableOpacity onPress={this._onPressItem.bind(this)}
        style={{flex:1, flexDirection:"row",
            paddingTop:SizeUtils.translateDesignHeightToCode(25),
            paddingBottom:SizeUtils.translateDesignHeightToCode(20),
            borderBottomWidth:SizeUtils.translateDesignNumToCode(3), borderColor:"#eeeeee",
            paddingLeft:SizeUtils.translateDesignWidthToCode(20),
            paddingRight:SizeUtils.translateDesignWidthToCode(20)}}>
          {this._renderShowImage(this.props.data)}
          <View style={{flex:1, flexDirection: "column", justifyContent:"space-between",
                paddingLeft:SizeUtils.translateDesignWidthToCode(20),
                paddingTop:SizeUtils.translateDesignHeightToCode(10),
                paddingBottom:SizeUtils.translateDesignHeightToCode(0),}}>
              <Text numberOfLines={2}
                style={[CommonStyles.primaryText, {color:'#000',
                  marginBottom:SizeUtils.translateDesignHeightToCode(10),
                  fontWeight:"bold",
                  fontSize:SizeUtils.translateFontSizeToCode(30),}]}>
                  {this.props.data.title}
              </Text>
              {this._newsBottomView()}
          </View>
      </TouchableOpacity>
    )
  }
  _newsBottomView=()=>(
    <View style={{flexDirection:'row',paddingTop:SizeUtils.translateDesignHeightToCode(20),alignItems: 'center',}}>
        <Text
            style={[CommonStyles.secondaryText, {flex:1,fontSize:SizeUtils.translateFontSizeToCode(24),
                }]}>
            {this._getDateSource(this.props.data)}
        </Text>
        <View style={{flexDirection:'row',alignItems: 'center',justifyContent: 'center',}}>
        <TImage source={require("../../../resource/images/home/kan.png")} 
            style={{marginLeft:10,width:34,height:34,}}/>
        <Text  style={[CommonStyles.secondaryText,{fontSize:SizeUtils.translateFontSizeToCode(24)}]}>
          {' '+this.state.readers_num}
        </Text>
        <Text  style={[CommonStyles.secondaryText,{marginLeft:10,fontSize:SizeUtils.translateFontSizeToCode(24)}]}>
          {DateUtils.formatDiffFromServerTime(this.props.data.posted_at)}
        </Text>
      </View>
    </View> 
  );
  _renderShowImage(data) {
    if (data.image_url || (data.image_urls && data.image_urls && data.image_urls.length > 0)) {
      return <TImage source={this._getImageSource(data)}
        style={{width: (750-50)/3, height:150}} resizeMode={"cover"} isMini={true}/>
    }
  }

  _getImageSource(data) {
    if(data.image_url) {
      return {uri:data.image_url};
    }
    else if (data.image_urls && data.image_urls && data.image_urls.length > 0) {
      return {uri:data.image_urls[0]};
    }
    else {
      return {uri:""};
    }
  }
  chageBrowse(read,like){
    console.log(read)
    this.setState({
      readers_num:read
    });
  }
  _getDateSource(data) {
    if(data.source) {
      return  data.source;
    }
    return "";
  }
  _onPressItem() {
    if (this.props.data.board_id!==undefined) {
      this.gotoPage(Config.pages.quanzi_post_content, {...this.props.data,isGongGao:true,chageBrowse:this.chageBrowse.bind(this)});
    }else{
      this.gotoPage(Config.pages.news_content, {...this.props.data,isGongGao:true,chageBrowse:this.chageBrowse.bind(this)});
    }
  }
}